package com.zzl.alligator.management.system.controller;

import com.zzl.alligator.framework.base.controller.BaseController;
import com.zzl.alligator.framework.support.web.vo.JsonResult;
import com.zzl.alligator.management.system.domain.SysDictData;
import com.zzl.alligator.management.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 字典数据表(SysDictData)控制层
 *
 * @author zhouziliang
 * @since 2020-06-30
 */
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController {
    
    /**
     * 服务对象
     */
    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 分页查询所有数据
     *
     * @param sysDictData 查询实体
     * @return 所有数据
     */
    @GetMapping
    public JsonResult findList(SysDictData sysDictData) {
        startPage();
        List<SysDictData> sysDictDataList = sysDictDataService.findSysDictDataList(sysDictData);
        return JsonResult.success(getDataTable(sysDictDataList));
    }

    @GetMapping("/type/{dictType}")
    public JsonResult findListByDictType(@PathVariable  String dictType) {
        List<SysDictData> list = sysDictDataService.findSysDictDataByType(dictType);
        return JsonResult.success(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param dictCode 主键
     * @return 单条数据
     */
    @GetMapping("/{dictCode}")
    public JsonResult findById(@PathVariable Long dictCode) {
        return JsonResult.success(sysDictDataService.findSysDictDataById(dictCode));
    }

    /**
     * 新增数据
     *
     * @param sysDictData 实体对象
     * @return 新增结果
     */
    @PostMapping
    public JsonResult add(@RequestBody SysDictData sysDictData) {
        return toAjax(this.sysDictDataService.insertSysDictData(sysDictData));
    }

    /**
     * 修改数据
     *
     * @param sysDictData 实体对象
     * @return 修改结果
     */
    @PutMapping
    public JsonResult update(@RequestBody SysDictData sysDictData) {
        return toAjax(this.sysDictDataService.updateSysDictData(sysDictData));
    }

    /**
     * 删除数据
     *
     * @param dictCode 主键
     * @return 删除结果
     */
    @DeleteMapping("/{dictCode}")
    public JsonResult delete(@PathVariable Long dictCode) {
        SysDictData dictDate = sysDictDataService.findSysDictDataById(dictCode);
        if(dictDate == null) {
            return JsonResult.success();
        }
        return toAjax(this.sysDictDataService.deleteById(dictDate.getDictType(), dictCode));
    }
}